/**
 * @author LKQ
 * @date 2022/1/14 10:18
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution solution = new FormalSolution();
        int[] nums = {1, 2, 3, 4};
        solution.productExceptSelf(nums);
    }
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] l = new int[n];
        int[] r = new int[n];
        int[] ans = new int[n];
        // nums[i] 左边的乘积
        l[0] = 1;
        for (int i = 1; i < n; i++) {
            l[i] = l[i-1] * nums[i-1];
        }
        // nums[i] 右边的乘积
        r[n-1] = 1;
        for (int j = n-2; j >= 0; j--) {
            r[j] = r[j+1] * nums[j+1];
        }
        for (int i = 0; i < n; i++) {
            ans[i] = l[i] * r[i];
        }
        return ans;
    }
}
